package com.orange.score.service;

import android.os.AsyncTask;
import android.util.Log;
import com.orange.common.android.activity.PPActivity;
import com.orange.score.model.config.ConfigManager;
import com.orange.score.model.league.LeagueManager;
import com.orange.score.model.match.FollowMatchManager;
import com.orange.score.model.match.MatchManager;
import com.orange.score.network.ResultCodeType;
import com.orange.score.network.ScoreNetworkRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MatchService
{
  private static final String TAG = "MatchService";
  FollowMatchManager followMatchManager;
  LeagueManager leagueManager;
  Timer liveScoreUpdateTimer;
  List<LiveUpdateChangeCallBack> liveUpdateChangeObserverList;
  LiveUpdateMatchTask liveUpdateMatchTask;
  MatchManager matchManager = new MatchManager();

  public MatchService()
  {
    this.leagueManager = new LeagueManager();
    this.followMatchManager = null;
    this.liveScoreUpdateTimer = null;
    this.liveUpdateMatchTask = null;
    this.liveUpdateChangeObserverList = new ArrayList();
    addScoreLiveUpdateObserver(this.matchManager);
  }

  private void liveScoreUpate()
  {
    this.liveUpdateMatchTask = new LiveUpdateMatchTask(this);
    LiveUpdateMatchTask localLiveUpdateMatchTask = this.liveUpdateMatchTask;
    String[] arrayOfString = new String[1];
    arrayOfString[0] = "";
    localLiveUpdateMatchTask.execute(arrayOfString);
  }

  private void stopLiveScoreUpdateTimer()
  {
    if (this.liveScoreUpdateTimer != null)
    {
      this.liveScoreUpdateTimer.cancel();
      this.liveScoreUpdateTimer = null;
    }
  }

  public void addScoreLiveUpdateObserver(LiveUpdateChangeCallBack paramLiveUpdateChangeCallBack)
  {
    this.liveUpdateChangeObserverList.add(paramLiveUpdateChangeCallBack);
  }

  public void addScoreLiveUpdateObserverAsFirst(LiveUpdateChangeCallBack paramLiveUpdateChangeCallBack)
  {
    this.liveUpdateChangeObserverList.add(0, paramLiveUpdateChangeCallBack);
  }

  public LeagueManager getLeagueManager()
  {
    return this.leagueManager;
  }

  public MatchManager getMatchManager()
  {
    return this.matchManager;
  }

  public void loadAllMatch(PPActivity paramPPActivity, MatchServiceCallBack paramMatchServiceCallBack)
  {
    LoadAllMatchTask localLoadAllMatchTask = new LoadAllMatchTask(this, paramPPActivity, paramMatchServiceCallBack);
    String[] arrayOfString = new String[1];
    arrayOfString[0] = "";
    localLoadAllMatchTask.execute(arrayOfString);
  }

  public void loadMatchDetail(String paramString, MatchServiceCallBack paramMatchServiceCallBack)
  {
    LoadMatchDetailTask localLoadMatchDetailTask = new LoadMatchDetailTask(this, paramString, paramMatchServiceCallBack);
    String[] arrayOfString = new String[1];
    arrayOfString[0] = "";
    localLoadMatchDetailTask.execute(arrayOfString);
  }

  public void removeScoreLiveUpdateObserver(LiveUpdateChangeCallBack paramLiveUpdateChangeCallBack)
  {
    this.liveUpdateChangeObserverList.remove(paramLiveUpdateChangeCallBack);
  }

  public void setFollowMatchManager(FollowMatchManager paramFollowMatchManager)
  {
    this.followMatchManager = paramFollowMatchManager;
  }

  public void startLiveScoreUpdateTimer()
  {
    startLiveScoreUpdateTimer(false);
  }

  public void startLiveScoreUpdateTimer(boolean paramBoolean)
  {
    if (this.liveScoreUpdateTimer != null)
    {
      this.liveScoreUpdateTimer.cancel();
      this.liveScoreUpdateTimer = null;
    }
    int i = 10;
    if (paramBoolean)
    {
      i = 0;
      this.liveScoreUpdateTimer = new Timer();
      this.liveScoreUpdateTimer.schedule(new TimerTask(this)
      {
        public void run()
        {
          Log.d("MatchService", "Fire live score update timer");
          MatchService.access$0(this.this$0);
        }
      }
      , i * 1000);
      return;
    }
    try
    {
      int j = Integer.parseInt(ConfigManager.getInstance().getRefreshInterval());
      i = j;
    }
    catch (Exception localException)
    {
    }
  }

  private class LiveUpdateMatchTask extends AsyncTask<String, Void, List<String[]>>
  {
    protected List<String[]> doInBackground()
    {
      ArrayList localArrayList;
      int i;
      Object localObject;
      String[] arrayOfString1 = ScoreNetworkRequest.getMatchLiveChange();
      if (arrayOfString1 == null)
        localObject = null;
      while (true)
      {
        return localObject;
        localArrayList = new ArrayList();
        i = 0;
        if (i < arrayOfString1.length)
          break;
        localObject = localArrayList;
      }
      if (arrayOfString1[i].length() == 0);
      while (true)
      {
        String[] arrayOfString2;
        while (true)
        {
          while (true)
            ++i;
          arrayOfString2 = arrayOfString1[i].split("\\^");
          if ((arrayOfString2 != null) && (arrayOfString2.length >= 12))
            break;
          Log.w("MatchService", "LiveUpdateMatch but fields count not enough, count = " + arrayOfString2.length);
        }
        localArrayList.add(arrayOfString2);
      }
    }

    protected void onPostExecute()
    {
      int i;
      int j;
      Log.d("MatchService", "LiveUpdateMatchTask onPostExecute");
      if ((paramList != null) && (paramList.size() > 0))
      {
        i = this.this$0.liveUpdateChangeObserverList.size();
        j = 0;
      }
      while (true)
      {
        if (j >= i)
        {
          this.this$0.startLiveScoreUpdateTimer();
          return;
        }
        ((LiveUpdateChangeCallBack)this.this$0.liveUpdateChangeObserverList.get(j)).notifyScoreLiveUpdate(paramList);
        ++j;
      }
    }

    protected void onPreExecute()
    {
    }
  }

  private class LoadAllMatchTask extends AsyncTask<String, Void, String[]>
  {
    PPActivity activity;
    MatchServiceCallBack callback;

    public LoadAllMatchTask(, PPActivity paramPPActivity, MatchServiceCallBack paramMatchServiceCallBack)
    {
      this.activity = paramPPActivity;
      this.callback = paramMatchServiceCallBack;
    }

    protected String[] doInBackground()
    {
      ConfigManager.getInstance();
      return ScoreNetworkRequest.getAllMatch(ConfigManager.getLanguage(), this.this$0.matchManager.getFilterScoreType());
    }

    protected void onCancelled()
    {
      Log.d("MatchService", "<LoadAllMatchTask>cancel task.");
    }

    protected void onPostExecute()
    {
      this.activity.hideDialog();
      Log.d("MatchService", "<MatchService> enter onPostExecute");
      if (isCancelled())
        Log.d("MatchService", "<MatchService> onPostExecut is cancel");
      while (true)
      {
        while (true)
        {
          return;
          if (paramArrayOfString != null)
            break;
          this.callback.loadAllMatchFinish(ResultCodeType.ERROR_NO_DATA);
        }
        int i = 1;
        if (paramArrayOfString.length == 1)
        {
          Log.w("MatchService", "no match/league data recv");
          i = 0;
        }
        String str1 = "";
        String str2 = "";
        String str3 = "";
        if ((i != 0) && (paramArrayOfString.length >= 3))
        {
          str2 = paramArrayOfString[1];
          str1 = paramArrayOfString[2];
          str3 = paramArrayOfString[0];
        }
        boolean bool = this.this$0.leagueManager.hasLeagueData();
        String[] arrayOfString1 = str2.split("\\!");
        this.this$0.leagueManager.updateDataFromStringList(arrayOfString1, 1);
        String[] arrayOfString2 = str1.split("\\!");
        this.this$0.matchManager.updateDataFromStringList(arrayOfString2, this.this$0.leagueManager, this.this$0.followMatchManager);
        if (!(bool))
          this.this$0.matchManager.selectAllLeagues(this.this$0.leagueManager.findAllLeagues());
        ConfigManager.getInstance().setServerDifferenceTime(str3);
        this.callback.loadAllMatchFinish(ResultCodeType.SUCCESS);
        this.this$0.startLiveScoreUpdateTimer();
      }
    }

    protected void onPreExecute()
    {
      this.activity.showProgressDialog("", "加载数据中", this);
    }
  }

  private class LoadMatchDetailTask extends AsyncTask<String, Void, String[]>
  {
    MatchServiceCallBack callback;
    String matchId;

    public LoadMatchDetailTask(, String paramString, MatchServiceCallBack paramMatchServiceCallBack)
    {
      this.matchId = paramString;
      this.callback = paramMatchServiceCallBack;
    }

    protected String[] doInBackground()
    {
      return ScoreNetworkRequest.getMatchDetail(this.matchId);
    }

    protected void onPostExecute()
    {
      if (paramArrayOfString == null)
        Log.w("MatchService", "get match detail from network null");
      while (true)
      {
        while (true)
        {
          return;
          if (paramArrayOfString.length >= 12)
            break;
          Log.w("MatchService", "get match detail data but length not enough, length = " + paramArrayOfString.length);
        }
        this.this$0.matchManager.updateMatchDetail(this.matchId, paramArrayOfString);
        this.callback.loadMatchDetailFinish(ResultCodeType.SUCCESS);
      }
    }
  }
}